
#include "reduce_sum_tiling.h"
#include "register/op_def_registry.h"

namespace optiling
{
    static ge::graphStatus TilingFunc(gert::TilingContext *context)
    {

        ReduceSumTilingData tiling;
        auto xshape = context->GetInputShape(0)->GetOriginShape();
        auto blockLength = context->GetInputShape(0)->GetOriginShape().GetShapeSize();
        auto axLength = context->GetInputShape(1)->GetOriginShape().GetShapeSize();
        int32_t dim_num = xshape.GetDimNum();
        int64_t shape[128];
        int64_t *ss = &shape[0];
        for (int i = 0; i < dim_num; i++)
        {
            ss[i] = xshape.GetDim(i);
        }
        context->SetBlockDim(1);
        tiling.set_blockLength(blockLength);
        tiling.set_axLength(axLength);
        tiling.set_dimNum(dim_num);
        tiling.set_shape(shape);
        tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
        context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
        return ge::GRAPH_SUCCESS;
    }
}

namespace ge
{
    static ge::graphStatus InferShape(gert::InferShapeContext *context)
    {
        const gert::Shape *x1_shape = context->GetInputShape(0);
        gert::Shape *y_shape = context->GetOutputShape(0);
        *y_shape = *x1_shape;
        return GRAPH_SUCCESS;
    }
}

namespace ops
{
    class ReduceSum : public OpDef
    {
    public:
        explicit ReduceSum(const char *name) : OpDef(name)
        {
            this->Input("x")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32, ge::DT_INT8})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
            this->Input("axis")
                .ParamType(REQUIRED)
                .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
            this->Output("y")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32, ge::DT_INT8})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
            this->Attr("keep_dims").AttrType(OPTIONAL).Bool(false);
            this->Attr("ignore_nan").AttrType(OPTIONAL).Bool(false);
            this->Attr("dtype").AttrType(OPTIONAL).String("float");

            this->SetInferShape(ge::InferShape);

            this->AICore()
                .SetTiling(optiling::TilingFunc);
            this->AICore().AddConfig("ascend310b");
        }
    };

    OP_ADD(ReduceSum);
}

